import { EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { ExcutePlan, RecmInfo, RoundInfo } from "../ExecutePlan";
import { IMoneyStrategy } from "../IMoneyStrategy";
import { FixedStrategy } from "../recmStrategy/FixedStrategy";
import { BossRabbitMoneyStrategy } from "./BossRabitMoneyStrategy";
import { PlanAV1StepStrategy } from "./WisdomMoneyStrategyV38Group";
import { FixedMoneyStrategy } from "./FixedMoneyStrategy";


export class WisdomMoneyStrategyV21Group implements IMoneyStrategy {
    subPlan: ExcutePlan[];
    debugCompoenent: string = "WisdomMoneyStrategyV21GroupDebug";
    strategyName: string;
    startIndex: number = -1;
    startLevel: number = 10000;
    recmV: EnumBjlResult = EnumBjlResult.U;
    recmMoney: number = 0;
    recmIndex: number = -1;
    resetMoney: number[] = [];
    protected _subPlanSum: number = 0;
    public get subPlanSum() {
        return this._subPlanSum;
    }
    constructor(name?: string) {
        this.strategyName = name ?? "GroupMoneyStrategy" + (new Date()).getMilliseconds() + parseInt((Math.random() * 1000).toString());
        this.subPlan = [];
        this.resetStrategy();
    }
    showMoney(roundInfo?: RoundInfo): [EnumBjlResult, number] {
        let zm = 0;
        let xm = 0;
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;

        this.subPlan.forEach(pl => {
            let plRoundInfo = Object.assign({}, roundInfo);
            //plRoundInfo.reverse = this.reverse;
            if (pl.running) {
                let [rv, rm] = pl.showMoney(plRoundInfo);
                pl.recmInfo.recmV = rv;
                pl.recmInfo.recmMoney = rm;

                if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                    zm += (pl.recmInfo.recmMoney * pl.multiple);

                }
                else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                    xm += (pl.recmInfo.recmMoney * pl.multiple);
                }
            }
            else {
                pl.recmInfo.recmV = EnumBjlResult.U;
                pl.recmInfo.recmMoney = 0;
            }


        });
        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }

        return [recmV, recmMoney];
    }
    resetRecm() {
        //this.subPlan.forEach(item => item.resetRecm());
        this.subPlan[0].moneyStrategy = [new PlanAV1StepStrategy("A计划", {
            baseMoney: 10,
            stopLFailCount: 3,
            failStepCount: 1,
            winStepCount: -1,
            stepMoney: 10,
        })];
        this.subPlan[0].toResult(new RoundInfo());

        this.subPlan[1].moneyStrategy = [new FixedMoneyStrategy("E计划", 30)];
        this.subPlan[1].toResult(new RoundInfo());

        this.subPlan[2].moneyStrategy = [new BossRabbitMoneyStrategy("F计划", {
            baseMoney: 10,
            winStep: 1,
            failStep: 0,
            prevMoney: 0,
            currentMoney: 10
        })];


        this.subPlan.forEach(plan => {
            plan.recmInfo.summaryInfo.resetSummaryInfo();
            plan.recmInfo.roundSummaryInfo.resetSummaryInfo();
            plan.userRecmInfo.summaryInfo.resetSummaryInfo();
            plan.userRecmInfo.roundSummaryInfo.resetSummaryInfo();
            plan.resetMoney = 0;
        })
    }

    resetStrategy(): void {
        this.recmV = EnumBjlResult.U;
        this.recmMoney = 0;
        this.recmIndex = -1;
        this.resetMoney = [];

        let planA = new ExcutePlan();
        planA.multiple = 3;
        planA.originMultiple = 3;
        planA.planName = "A计划";
        planA.baseMoney = 10;
        planA.recmStrategy = [new FixedStrategy({ reverse: false })];
        planA.moneyStrategy = [new PlanAV1StepStrategy("A计划", {
            baseMoney: 10,
            stopLFailCount: 3,
            failStepCount: 1,
            winStepCount: -1,
            stepMoney: 10,
        })];
        planA.toResult(new RoundInfo());

        let planE = new ExcutePlan();
        planE.multiple = 1;
        planE.originMultiple = 1;
        planE.planName = "E计划";
        planE.baseMoney = 30;
        planE.recmStrategy = [new FixedStrategy({ reverse: false })];
        planE.moneyStrategy = [new FixedMoneyStrategy("E计划", 30)];
        planE.toResult(new RoundInfo());

        let planF = new ExcutePlan();
        planF.multiple = 1;
        planF.originMultiple = 1;
        planF.planName = "F计划";
        planF.baseMoney = 10;
        planF.running = false;
        planF.recmStrategy = [new FixedStrategy({ reverse: false })];
        planF.moneyStrategy = [new BossRabbitMoneyStrategy("F计划", {
            baseMoney: 10,
            winStep: 1,
            failStep: 0,
            prevMoney: 10,
            currentMoney: 10
        })];
        planF.toResult(new RoundInfo());


        this.subPlan.push(planA);
        this.subPlan.push(planE);
        this.subPlan.push(planF);
    }
    setSubResult(pl: ExcutePlan, roundInfo: RoundInfo) {
        pl.setResult(roundInfo, pl.recmInfo, pl.multiple, pl.waterRate, pl.fuliRate);

        let m = pl.recmInfo.summaryInfo.winMoney - pl.recmInfo.summaryInfo.failMoney;
        this._subPlanSum += m;

        let userRecmV = pl.toResult(roundInfo);
        let newRoundInfo: RoundInfo = Object.assign({}, roundInfo, { userRecm: userRecmV });
        pl.userRecmInfo.recmV = userRecmV;
        return newRoundInfo;
    }
    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;
        let zm = 0;
        let xm = 0;

        this.subPlan.forEach(pl => {
            let plRoundInfo = this.setSubResult(pl, roundInfo);

            let planF = this.subPlan[2];
            if (pl.planName == "E计划") {
                if (pl.recmInfo.summaryInfo.lwin >= 2) {
                    if (!planF.running) {
                        planF.moneyStrategy = [new BossRabbitMoneyStrategy("F计划", {
                            baseMoney: 10,
                            winStep: 1,
                            failStep: 0,
                            prevMoney: 0,
                            currentMoney: 10
                        })];
                        planF.running = true;
                    }

                }
                else if (planF.running) {
                    let planFWf = planF.recmInfo.summaryInfo.winMoney - planF.recmInfo.summaryInfo.failMoney;
                    planF.resetStrategy();
                    planF.resetMoney = planFWf;
                    planF.running = false;
                }

                if (recmInfo.summaryInfo.winCount < recmInfo.summaryInfo.failCount) {
                    pl.running = false;
                }
                else {
                    pl.running = true;
                }
            }

            if (pl.planName == "A计划" && this.resetMoney.length > 0) {
                let resetMoney = this.resetMoney[this.resetMoney.length - 1];
                let winMoney = pl.recmInfo.roundSummaryInfo.winMoney - pl.recmInfo.roundSummaryInfo.failMoney;
                if (winMoney > resetMoney) {
                    this.resetMoney.pop();
                    // this.subPlan.forEach(item => {
                    //     item.multiple = item.originMultiple * (this.resetMoney.length + 1);
                    //     item.recmInfo.recmMoney = item.baseMoney;
                    // });
                    pl.resetRecm();
                    pl.recmInfo.roundSummaryInfo.resetSummaryInfo();
                    pl.multiple = pl.originMultiple + (this.resetMoney.length)

                }
            }

            if (pl.running) {

                let [rv, rm] = pl.toMoney(plRoundInfo, pl.recmInfo, pl.userRecmInfo, pl.baseMoney);
                pl.recmInfo.preBetCount = pl.recmInfo.betCount;
                pl.recmInfo.recmV = rv;

                let wf = pl.recmInfo.roundSummaryInfo.failMoney - pl.recmInfo.roundSummaryInfo.winMoney;
                if (pl.planName == "A计划" && rm >= 100 && wf > 0) {
                    this.resetMoney.push(wf);
                    pl.resetRecm();
                    pl.recmInfo.roundSummaryInfo.resetSummaryInfo();
                    rm = pl.baseMoney;
                    pl.multiple = pl.originMultiple + (this.resetMoney.length)
                }

                pl.recmInfo.recmMoney = rm;

                if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                    zm += (pl.recmInfo.recmMoney * pl.multiple);

                }
                else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                    xm += (pl.recmInfo.recmMoney * pl.multiple);
                }

            }
            else {
                pl.recmInfo.recmV = EnumBjlResult.U;
                pl.recmInfo.recmMoney = 0;
            }

        })
        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }

        this.recmV = recmV;
        this.recmMoney = recmMoney;
        return [recmV, recmMoney];
    }

    copyValue(valueObj: any): void {
        var obj = valueObj as WisdomMoneyStrategyV21Group;
        if (obj) {
            this.debugCompoenent = obj.debugCompoenent;
            this.startIndex = obj.startIndex;
            this._subPlanSum = obj._subPlanSum;
            this.recmV = obj.recmV;
            this.resetMoney = obj.resetMoney;
            this.recmMoney = obj.recmMoney;
            this.recmIndex = obj.recmIndex;

            for (let i = 0; i < this.subPlan.length; i++) {
                this.subPlan[i].copyValue(obj.subPlan[i]);
            }
        }
    }
}